package organization;

import java.util.ArrayList;
import java.util.Scanner;

import person.*;

public  class Base  {

	private static Arrays list = new Arrays ();

	public static <T extends PersonInfo> void addPerson (T obj) {
		list.personBase.add(obj);
		System.out.println("Person added to base."+obj.getFirstAndScndName());
		
	}
	public static Arrays getList(){
		return list;
	}
	public static <T extends PersonInfo> ArrayList <T> getSortlist (T obj){
		ArrayList <T> sorted = new ArrayList<T> ();
		
		if (obj==null){
		System.out.println("Person size "+list.personBase.size()+"IS NULL!!");
		}
		for (int i=0;i<list.personBase.size();i++){
			if (list.personBase.get(i).getClass().isInstance(obj)){
				sorted.add((T)list.personBase.get(i));
			}
		}
		return sorted;
	}
	public static <T extends PersonInfo> T getPersonByName (String frstName,String scndName,T obj){
		ArrayList <T> sorted = getSortlist (obj);
		for (int i=0;i<sorted.size();i++){
			if (frstName.equals(sorted.get(i).getFirstName())){
				if (scndName.equals(sorted.get(i).getSecondName())){
					return sorted.get(i);
				}
			}
		}
		System.out.println("Person not Found!");
		return null; 		
	}
	public static boolean addAudit (int number, int floor){
		Auditorium input = new Auditorium (number,floor);
		
		for (int i=0;i<list.auditBase.size();i++){
			if (input.getNumber() == list.auditBase.get(i).getNumber()){
				System.out.println("Room with this number already exists!");
				return false;
			}
		}
		list.auditBase.add(input);
		System.out.println("Auditorium "+input.getNumber()+" on floor "+input.getFloor()+" added.");
		return true;
	}
	public static Auditorium getAudObjByNumber (int number){

		for (int i=0;i<list.auditBase.size();i++){
			if (list.auditBase.get(i).getNumber() == number){
				System.out.println("Floor "+list.auditBase.get(i).getFloor()+". Welcome!");
				return list.auditBase.get(i);
			}
		}
		System.out.println("Auditorium not found!");
		return null;
	}
	public static boolean checkUniq (PersonInfo input){
		for (int i=0;i<list.personBase.size();i++){
			if (input.getFirstAndScndName().equals(list.personBase.get(i).getFirstAndScndName())){
				if (input.getPlaceOfBirth().equals(list.personBase.get(i).getPlaceOfBirth())){
					return false;
				}
			}
		}
		return true;
	}
	public static void printFreeAudit (){
		for (int i=0;i<list.auditBase.size();i++){
			if (list.auditBase.get(i).getStatus() == true){
			System.out.println("Auditorium "+list.auditBase.get(i).getNumber()+" floor "+list.auditBase.get(i).getFloor());
			}
		}
	}
	public static void printAllAudit (){
		if (list.auditBase.size()==0){
			System.out.println("Auditoriums not found. Please add auditoriums in auditoriums menu.");
		}else{
			for (int i = 0; i < list.auditBase.size(); i++) {
				System.out.println("Auditorium number"
						+ list.auditBase.get(i).getNumber() + " floor "
						+ list.auditBase.get(i).getFloor());
			}
		}
	}
	public static boolean addLesson (Lesson lesson){
		for (int i=0;i<list.lessonBase.size();i++){
			if (lesson.getLessonTime().equals(list.lessonBase.get(i).getLessonTime())){
				if (lesson.getAuditNumber() == list.lessonBase.get(i).getAuditNumber()){
					System.out.println("Auditorium Busy in this time.");
					return false;
				}
			}
		}
		list.lessonBase.add(lesson);
		System.out.println("Lesson "+lesson.getLessonName()+" audit."+lesson.getAuditNumber()+" group "+lesson.getGroup().getGrpName()+"added");
		return true;
	}
	public static ArrayList <Lesson> getGroupLessons (Group group){
		ArrayList <Lesson> grpLessonBase = new ArrayList <Lesson>();
		for (int i=0;i<list.lessonBase.size();i++){
			if (list.lessonBase.get(i).getGroup().getGrpID() == group.getGrpID()){
				grpLessonBase.add(list.lessonBase.get(i));
			}
		}
		return grpLessonBase;
	}
	public static void getGroupCurculum (){
		System.out.println("Please choose group:");
		ArrayList <Group> grp = new output.StudentConsole().getGroupList();
		if (grp !=null){
			Scanner scan = new Scanner (System.in);
			while (scan.hasNextLine()){
				if (scan.hasNextInt()){
					int opt = scan.nextInt();
					if (opt > 0 & opt <=grp.size()){
						ArrayList <Lesson> lesson = getGroupLessons(grp.get(opt));
						if (lesson.size()>=1){
							for (int i=0;i<lesson.size();i++){
								System.out.println((i+1)+". "+lesson.get(i).getLessonName()+";Teacher "+lesson.get(i).getTeachName()+"; Auditorium "+lesson.get(i).getAuditNumber());
							}
						}else{
							System.out.println("Lessons not found");
							return;
						}
					}else {
						System.out.println("Incorrect option.");
					}
				}else{
					System.out.println("Should be numeric!");
				}
					
			}
		}else{
			return;
		}
		
	}
	public static void saveBase () {
		list.saveBase();
	}
	public static void openBase() {
		list.openBase();
	}
}
